import 'package:enjoy_plus/pages/home/index.dart';
import 'package:enjoy_plus/pages/mine/index.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

import '../utils/eventbus.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  int currentIndex = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initToken();
  }

  initToken() async {
    await TokenManager().init();
    eventBus.on<LogoutEvent>().listen((event) {
      // 跳转登录页面
      Navigator.pushNamed(context, '/login');
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: currentIndex,
        children: [
          HomePage(),
          MinePage(currentIndex: currentIndex),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
          onTap: (index) {
            setState(() {
              currentIndex = index;
            });
          },
          currentIndex: currentIndex,
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Color.fromARGB(255, 99, 144, 172),
          items: [
            BottomNavigationBarItem(
              icon: Image.asset(
                'assets/tabs/home_default.png',
                width: 32,
                height: 32,
              ),
              activeIcon: Image.asset(
                'assets/tabs/home_active.png',
                width: 32,
                height: 32,
              ),
              label: '首页',
            ),
            BottomNavigationBarItem(
              icon: Image.asset(
                'assets/tabs/my_default.png',
                width: 32,
                height: 32,
              ),
              activeIcon: Image.asset(
                'assets/tabs/my_active.png',
                width: 32,
                height: 32,
              ),
              label: '我的',
            ),
          ]),
    );
  }
}
